<?php

namespace addons\official\model;

use think\Cache;
use think\Model;

class OfficialBaseModel extends Model
{

    protected $language = 2;

    public function __construct($data = [])
    {
        $this->language = cookie('language') ? cookie('language') : 2;
        parent::__construct($data);
    }

    /**
     * 修改图片路径
     * @param string $value
     * @param array $data
     * @return string
     */
    public function getImageAttr($value)
    {
        return cdnurl($value);
    }


    /**
     * 查询栏目所属表
     */
    public function queryTableName($category_id)
    {
        //查询栏目
        $tree_category_all = $this->getAll0fficiaCategory();
        foreach ($tree_category_all as $item) {
            foreach ($item['son'] as $son) {
                if ($son['id'] == $category_id) {
                    $category = $son;
                }
            }
            if ($item['id'] == $category_id) {
                unset($item['son']);
                $category = $item;
            }
        }
        //生成配置文件
        $official_rule_url = ADDON_PATH . 'official' . DS . 'controller' . DS . 'common' . DS . 'official_rule.php';
        if (!file_exists($official_rule_url)) {
            $official_rule = [];
            $rule_all = db('official_rule')->select();
            foreach ($rule_all as $item) {
                $official_rule[$item['id']] = $item;
            }
            file_put_contents(
                $official_rule_url,
                '<?php' . "\n\nreturn " . var_export_short($official_rule) . ";\n"
            );
        } else {
            $official_rule = include $official_rule_url;
        };
        $official_rule = $official_rule[$category['official_rule_id']] ?? '';
        if (!$official_rule) {
            return '';
        }
        //返回数据表名称
        $str = str_replace('_', ' ', $official_rule['table_name']);
        $str = preg_replace('# #', '', ucwords($str));
        return $str;
    }


    /**
     * 查询所有栏目
     */
    private function getAll0fficiaCategory()
    {
        //生成配置文件
        $official_tree_category_url = ADDON_PATH . 'official' . DS . 'controller' . DS . 'common' . DS . 'official_tree_category.php';
        if (!file_exists($official_tree_category_url)) {
            $official_category = (new OfficialCategory())
                ->with(['son'])
                ->where('pid', 0)
                ->field(['content', 'e_content'], true)
                ->where('deletetime', null)
                ->order('weigh', 'desc')
                ->select();
            $tree_category_all = [];
            foreach ($official_category as $item) {
                $tree_category_all[] = $item->toArray();
            }
            file_put_contents(
                $official_tree_category_url,
                '<?php' . "\n\nreturn " . var_export_short($tree_category_all) . ";\n"
            );
        } else {
            $tree_category_all = include $official_tree_category_url;
        }
        return $tree_category_all;
    }
}